//
//  LocalFileModel.m
//  DAY18_综合讲解
//
//  Created by wanli on 14/12/18.
//  Copyright (c) 2014年 wanli. All rights reserved.
//

#import "LocalFileModel.h"

@implementation LocalFileModel

+ (NSString *)libraryPathWithName:(NSString *)fileName{
    NSString *path = [NSString stringWithFormat:@"%@/%@", [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject], fileName];
    return path;
}

+ (void)saveFile:(NSData *)data name:(NSString *)name{
//    NSString *path = [NSString stringWithFormat:@"%@/%@", [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)firstObject], name];
    NSString *path = [LocalFileModel libraryPathWithName:name];
    [data writeToFile:path atomically:YES];
}

+ (NSData *)readFile:(NSString *)name{

    BOOL exist = [LocalFileModel fileExists:name];
    if (exist) {
        NSString *path = [LocalFileModel libraryPathWithName:name];
        NSData *data = [NSData dataWithContentsOfFile:path];
        return data;
    }
    return [NSData data];
}

+ (BOOL)fileExists:(NSString *)fileName{
    NSFileManager *manager = [NSFileManager defaultManager];
    NSString *path = [LocalFileModel libraryPathWithName:fileName];
    return [manager fileExistsAtPath:path];
}





@end
